/**
 * Copyright &copy; 2017-2018 <a href="http://www.webcsn.com">webcsn</a> All rights reserved.
 *
 * @author hermit
 * @date 2018-04-17 10:54:58
 */
package com.wxmp.wxcms.ctrl;

import com.wxmp.core.common.BaseCtrl;
import com.wxmp.core.util.AjaxResult;
import com.wxmp.core.util.DataUtil;
import com.wxmp.core.util.DateUtil;
import com.wxmp.core.util.MyGetLocation;
import com.wxmp.wxcms.domain.RegisterUser;
import com.wxmp.wxcms.domain.UserCalc;
import com.wxmp.wxcms.service.RegisterUserService;
import com.wxmp.wxcms.service.UserCalcService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 *
 * @author fuzi kong
 * @version 2.0
 * @date 2018-06-03 10:54:58
 */
@Controller
@RequestMapping("userCalc")
public class UserCalcCtrl extends BaseCtrl {

	private Logger logger=Logger.getLogger(UserCalcCtrl.class);
	@Autowired
	private UserCalcService userCalcService;
	@Autowired
	private RegisterUserService registerUserService;

	/**
	 * 分页查询
	 * @param searchEntity
	 * @return
	 */
	@RequestMapping(value = "/listForPage")
	@ResponseBody
	public AjaxResult listForPage(UserCalc searchEntity) {
		List<UserCalc> list = userCalcService.getUserClacListByPage(searchEntity);
		if (CollectionUtils.isEmpty(list)) {
			return AjaxResult.success();
		}
		return getResult(searchEntity,list);
	}
	
	/**
	 * 全国算法
	 * @param userCalc
	 * @return
	 */
	@RequestMapping(value = "/calc",produces = "application/json; charset=UTF-8",method=RequestMethod.POST)
	@ResponseBody
	public AjaxResult  calc(UserCalc userCalc) {
		logger.debug("===[UserCalcCtrl.calc]===计算传入参数信息===" + userCalc);
		
		//验证是否绑定手机号
		boolean bind = registerUserService.bindPhone(userCalc.getOpenid());
		if(!bind) {
			return AjaxResult.failure("没有绑定手机号,请绑定手机号。","1");
		}
		//验证费用是否到期
		boolean feeDate =validateDate(userCalc.getOpenid()) ; 
		logger.debug("===[UserCalcCtrl.calc]===方法费用是否到期=="+feeDate);
		if( feeDate) {
		        Map<String,String> resultMap = new HashMap<String,String>();
		        
		        Double width = (userCalc.getWidth() !=null && !"undefined".equals(userCalc.getWidth())) ? userCalc.getWidth()  : 0;
		        Double density = (userCalc.getDensity() !=null && !"undefined".equals(userCalc.getDensity()) )? userCalc.getDensity() :0;
		        Double coverplateThick = (userCalc.getCoverplateThick() !=null && !"undefined".equals(userCalc.getCoverplateThick())) ? userCalc.getCoverplateThick() : 0;
		        Double coverplatePrice = (userCalc.getCoverplatePrice() !=null && !"undefined".equals(userCalc.getCoverplatePrice())) ? userCalc.getCoverplatePrice() : 0;
		        Double bottomplateThick = (userCalc.getBottomplateThick() !=null && !"undefined".equals(userCalc.getBottomplateThick())) ?userCalc.getBottomplateThick() : 0;
		        Double bottomplatePrice = (userCalc.getBottomplatePrice() != null && !"undefined".equals(userCalc.getBottomplatePrice()))?userCalc.getBottomplatePrice() : 0;
		        Double height = (userCalc.getHeight() != null && !"undefined".equals(userCalc.getHeight()))? userCalc.getHeight() :0;
		        Integer partitionplateNum = (userCalc.getPartitionplateNum() != null && !"undefined".equals(userCalc.getPartitionplateNum())) ? userCalc.getPartitionplateNum() : 0;
		        Double partitionplateThick = (userCalc.getPartitionplateThick() != null && !"undefined".equals(userCalc.getPartitionplateThick()) )?userCalc.getPartitionplateThick() :0;
		        Double partitionplatePrice = (userCalc.getPartitionplatePrice() !=null && !"undefined".equals(userCalc.getPartitionplatePrice())) ? userCalc.getPartitionplatePrice() : 0;
		        
		        int addCover = 20, addBottom = 20;
		        if( coverplateThick >= 1.5 ||  width >=600  ) {
		        	addCover = 30;
		        }
		        if(bottomplateThick >=1.5 || width >=600) {
		        	addBottom = 30;
		        }
		 
		        
		        //计算盖板，底板，隔板总价
				Double coverTotal  = (width + addCover)* density * coverplateThick * coverplatePrice ;
				Double botttomTotal = (width + addBottom + height * 2) * density * bottomplateThick * bottomplatePrice ;
				Double partitionTotal = height * partitionplateNum * density * partitionplateThick * partitionplatePrice;
				
				//槽式价格
				Double trenchPrice =( coverTotal + botttomTotal + partitionTotal) /1000000;
				//梯式价格
				//1. 底板价格为0，并且隔板价格为0；就是单独算盖板，应该价格一样
				Double laddertypePrice;
				if (botttomTotal ==0 && partitionTotal ==0){
					laddertypePrice = coverTotal /1000000;
				}else{
					//2.盖板和底板九折
					laddertypePrice =( coverTotal  * 0.9 + botttomTotal * 0.9+ partitionTotal) /1000000;
				}

				//放入集合
				resultMap.put("trenchPrice",  trenchPrice < 1 ? "0" + DataUtil.getDouble(trenchPrice) : DataUtil.getDouble(trenchPrice));
				resultMap.put("laddertypePrice", laddertypePrice<1 ? "0" + DataUtil.getDouble(laddertypePrice):DataUtil.getDouble(laddertypePrice));
				
				//存入数据库
				userCalc.setTrenchPrice(trenchPrice);
				userCalc.setLaddertypePrice(laddertypePrice);
				//获取用户ID外键
				RegisterUser registerUser = registerUserService.getByOpenId(userCalc.getOpenid());
				userCalc.setUserid(registerUser.getId());
				userCalcService.insert(userCalc);
				return AjaxResult.success(resultMap,"计算成功。","3");
		}else {
			    return AjaxResult.failure("费用到期，请续费。","2");
		}
		
	}
	/**
	 * 验证用户的费用是否到期
	 * @param userid
	 * @return
	 */
	private boolean validateDate(String openid) {
		logger.debug("===[UserCalcCtrl.validateDate]===openid=="+openid);
		boolean flag = false;
		RegisterUser regUser = registerUserService.getByOpenId(openid);
		Date endTime = regUser.getEndTime();//结束时间
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.HOUR,8);
		Date nowTime = new Date(calendar.getTimeInMillis());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		logger.debug("===[UserCalcCtrl.validateDate]===endTime=="+sdf.format(endTime));
		logger.debug("===[UserCalcCtrl.validateDate]===nowTime=="+sdf.format(nowTime));
		logger.debug("===[UserCalcCtrl.validateDate]===nowTime.before(endTime)=="+nowTime.before(endTime));
		
		if(nowTime.before(endTime)) {
			flag = true;
		}
		return flag;
	}
}
